using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Windows;
using System.Windows.Input;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;

namespace WozLib.UI.Ink.SketchCommands
{
  public class MouseMoveSketchCommand : SketchCommand
  {
    [XmlElement("Position")]
    public Point Position
    {
      get
      {
        return _Position;
      }
      set
      {
        _Position = value;
      }
    } private Point _Position;

    [XmlAttribute("LeftButton")]
    public MouseButtonState LeftButton
    {
      get
      {
        return _LeftButton;
      }
      set
      {
        _LeftButton = value;
      }
    } private MouseButtonState _LeftButton = MouseButtonState.Released;

    [XmlAttribute("MiddleButton")]
    public MouseButtonState MiddleButton
    {
      get
      {
        return _MiddleButton;
      }
      set
      {
        _MiddleButton = value;
      }
    } private MouseButtonState _MiddleButton = MouseButtonState.Released;

    [XmlAttribute("RightButton")]
    public MouseButtonState RightButton
    {
      get
      {
        return _RightButton;
      }
      set
      {
        _RightButton = value;
      }
    } private MouseButtonState _RightButton = MouseButtonState.Released;

    public MouseMoveSketchCommand()
      : base()
    {
    }

    public MouseMoveSketchCommand(Point position, MouseButtonState leftButton, MouseButtonState middleButton, MouseButtonState rightButton)
    {
      Position = position;
      LeftButton = leftButton;
      MiddleButton = middleButton;
      RightButton = rightButton;
    }

    public override void Redo()
    {
      if (this.WozClientCommandReferences != null)
      {
        if (this.WozClientCommandReferences.InkCanvas != null)
        {
          this.WozClientCommandReferences.InkCanvas.Overlay(Position, LeftButton, MiddleButton, RightButton);
        }
        if (this.WozClientCommandReferences.InkOverlayCanvas != null)
        {
          lock (this.WozClientCommandReferences.InkOverlayCanvas)
          {
            if (this.WozClientCommandReferences.InkOverlayEllipse == null)
            {
              this.WozClientCommandReferences.InkOverlayEllipse = new Ellipse();

              // just want a local ref for intellisense to work
              Ellipse ellipse = this.WozClientCommandReferences.InkOverlayEllipse;

              ellipse.Width = 5;
              ellipse.Height = 5;
              ellipse.Fill = Brushes.Red;
              ellipse.Visibility = Visibility.Hidden;
              this.WozClientCommandReferences.InkOverlayCanvas.Children.Add(ellipse);
            }
          }

          if (LeftButton == MouseButtonState.Released)
          {
            Ellipse ellipse = this.WozClientCommandReferences.InkOverlayEllipse;
            ellipse.SetValue(Canvas.LeftProperty, Position.X - (ellipse.Width / 2));
            ellipse.SetValue(Canvas.TopProperty, Position.Y - (ellipse.Height / 2));
            ellipse.Visibility = Visibility.Visible;
          }
          else
          {
            Ellipse ellipse = this.WozClientCommandReferences.InkOverlayEllipse;
            ellipse.Visibility = Visibility.Hidden;
          }
        }
      }
    }

    public override void Undo()
    {
      // not really anything to undo for this command - i can't un-move the mouse (unless we tracked previous position)
    }
  }
}
